# 获取外网源地址中，包列表
# 输入：源地址一行
# 输出：包列表
import sys
import urllib.parse

from lxml import html

import requests
from bs4 import BeautifulSoup


class OnlinePackList:
    def __init__(self, url: str):
        if url.endswith("/"):
            self.url = url + 'Packages'
        else:
            self.url = url + '/Packages'

    @property
    def packlist(self):
        response = requests.get(self.url)
        if response.status_code == 200:
            html_content = response.text
            # soup = BeautifulSoup(html_content, 'html.parser')
            # html = soup.prettify()
        else:
            print("外网源访问错误")
            sys.exit(1)
        decoded_html = urllib.parse.unquote(html_content, encoding="UTF-8")
        packlist = []
        # sys.exit(0)
        tree = html.fromstring(html_content)
        links = tree.xpath('//a/@href')
        for mumber in links:
            if mumber.endswith(".rpm"):
                packlist.append(mumber.replace("%2B", "+"))
        # print(packlist)
        # print("外网源包总计：", len(packlist))
        return packlist
